﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// </summary>
    public partial class TWBDAT
    {

        public TWBDAT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBDAT();
            this.DataContext = MV;
            MESSAGES += "WBBS10;";
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);

        }
        public override void OnWFChg()
        {

            base.OnWFChg();
            if (dwWF == null)
            {
                WAART = "";
            }
            else
            {
                WAART = WAKZ + CART;
            }
        }
        public override void OnWSChg()
        {
            if (dwWS == null)
                return;
            if (WAKZ == "R")
            {
                ACTRLFD = double.Parse(dwWS["WARGRLFD"].ToString());

            }
            else
            {
                ACTRLFD = double.Parse(dwWS["WAFGRLFD"].ToString());
            }
            base.OnWSChg();
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ACTRLFD = 0;
                return;
            }
            PM["CART"] = CART;
            PM["CFARBE"] = CFARBE;
            PM["CBESCHICH"] = "".PadRight(10);
            PM["CDESS"] = "".PadRight(10); ;
            PM["CCOLORIT"] = "".PadRight(10); ;

            PM["WBRWKRS"] = "  ";
            PM["LO"] = LO;
            PM["LB"] = LB;
            PM["LP"] = LP;
            PM["ORDERNUM"] = "".PadRight(12);
            PM["ACTRLFD"] = ACTRLFD;
            if (WAKZ == "")
                WAKZ = "F";
            PM["WAKZ"] = WAKZ;
            PM["ACTRLFD"] = ACTRLFD;
            if (VKDELDATE == "")
                VKDELDATE = TMV.date;
            PM["VKDELDATE"] = VKDELDATE; 
            TMV.NEWWB04(PM);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            PM["RWKRS"] = dwMaster["WBRWKRS"].ToString();
            PM["RWLFD"] = dwMaster["WBRWLFD"].ToString();
            TMV.DELWBDAT(PM);
            filter = "WBRWKRS='" + dwMaster["WBRWKRS"].ToString() + "' AND WBRWLFD='" + dwMaster["WBRWLFD"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1);
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        #region====WorkFlow========
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WBBS10":
                    ENTRY(msg);
                    break;
            }
        }
        public void ENTRY(MessageInfo msg)
        {
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwMaster["WBMTR"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwMaster["WBMTR"] = current + increased;
            dwMaster["WBMTRK"] = dwMaster["WBMTR"];
            current = Convert.ToDouble(dwMaster["WBSTK"].ToString());
            dwMaster["WBSTK"] = current + 1;
            TMV.UpdateWBDAT(dwMaster, app.company, 0);
        }
        #endregion

        public void STKNR(DataRow _dwData)
        {
            _dwData["WBBPOS"] = dwWB04["WBLESTKNR"];
            _dwData["WBBSTKNR"] = dwWB04["WBRWKRS"].ToString().Trim() + Helpers.Helper.GetSubRollNum(dwWB04["WBRWLFD"].ToString().Trim()) + "-" + Helpers.Helper.GetSubRollNum(dwWB04["WBLESTKNR"].ToString().Trim());
            dwWB04["WBLESTKNR"] = Helper.Right("0000" + (System.Convert.ToDouble(dwWB04["WBLESTKNR"].ToString()) + 1).ToString(), 4);
            _dwData["WBBURPOS"] = _dwData["WBBPOS"];
            Update();
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
            {
                return;
            }
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBRWKRS"] = ORDERNUM.Substring(0, 2);
            dwData["WBRWLFD"] = ORDERNUM.Substring(2, 6);
            SResult = ((VWBDAT)MV).QueryWBDAT(dwData, 1);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                Update();
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void OnReload(bool value)
        {

            if (!value)
                return;
            if (KEY.Length < 8)
                return;
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBRWKRS"] = KEY.Substring(0, 2);
            dwData["WBRWLFD"] = KEY.Substring(2, 6);
            SResult = TMV.QueryWBDAT(dwData, 1);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            dwMaster.ItemArray = TBTemp.Rows[0].ItemArray;
            IsSaved = false;
            IsSaved = true;
            base.OnReload(value);
        }

    }
}
